package com.student.Activitys.http.manager;

import android.app.Activity;
import android.util.Log;

import com.student.Activitys.bean.LeaveDetailsBean;
import com.student.Activitys.bean.LeaveParameterBean;
import com.student.Activitys.bean.InfoReturnBean;
import com.student.Activitys.bean.ScheduleWeekBean;
import com.student.Activitys.bean.pack.ScheduleJsonPack;
import com.student.Activitys.bean.pack.ScheduleTestingJsonPack;
import com.student.Activitys.bean.pack.ScheduleWeekDateJsonPack;
import com.student.Activitys.http.ApiService;
import com.student.Activitys.http.NetWorkMgr;
import com.student.Activitys.http.api.ScheduleAPI;
import com.student.Activitys.utils.Constant;

import org.simple.eventbus.EventBus;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 项目名称：
 * 类描述：所有 课程表 接口管理 manager
 * 创建人：张飞祥
 * 创建时间：2017/9/27 0027 下午 1:33
 * 修改人：张飞祥
 * 修改时间：2017/9/27 0027 下午 1:33
 * 修改备注：
 */
public class ScheduleManager {

    private static ScheduleAPI mScheduleAPI;
    private static ApiService mApiService;
    private static Activity mActivity;
    private static int mTaskId;

    //加载数据类型
    private int RefType= Constant.FIRST_LOAD;
    //数据加载总长度
    private static  int size;

    private static class ScheduleApiManagerHolder {
        private static final ScheduleManager INSTANCE = new ScheduleManager();
    }
    public static final ScheduleManager getScheduleApiManager(Activity activity, int taskId) {
        mScheduleAPI = NetWorkMgr.getInstance(activity)
                .getServiceAPI(ScheduleAPI.class);
        mApiService = ApiService.getApiService();
        mActivity = activity;
        mTaskId = taskId;
        return ScheduleApiManagerHolder.INSTANCE;
    }
    public void getType(int type){
        RefType=type;
    }


    /**
     *
     * 日课表 数据处理请求
     *
     */
    public void scheduleDayList(String action,String xh,String nowDate) {
        Subscription subscription = mScheduleAPI
                .scheduleDayList(action,xh,nowDate).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ScheduleJsonPack>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_DAY_SCHEDULE_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"scheduleDayList(日课表) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(ScheduleJsonPack jsonPack) {
                        //返回数据处理
                        if (jsonPack != null)
                            EventBus.getDefault().post(jsonPack, Constant.BUS_DAY_SCHEDULE_LIST);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 日课表 请假申请 数据处理请求
     *
     */
    public void scheduleDayLeave(String action,String qjly,String jsh,String skjc,String kch,
                                String skzc,String skxq,String xh,String xnxq,String qjlx,String qjrq) {
        Subscription subscription = mScheduleAPI
                .scheduleDayLeave(action,qjly,jsh,skjc,kch,skzc,skxq,xh,xnxq,qjlx,qjrq).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LeaveParameterBean>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_DAY_LEAVE_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"dayLeave(请假申请) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(LeaveParameterBean bean) {
                        //返回数据处理
                        if (bean != null)
                            EventBus.getDefault().post(bean, Constant.BUS_DAY_LEAVE_LIST);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }
    /**
     *
     * 日课表 请假详情 数据处理请求
     *
     */
    public void scheduleDayLeaveDetails(String action,String xh,String kch,String skjc,String skxq,
                                String skzc,String xnxq) {
        Subscription subscription = mScheduleAPI
                .scheduleDayLeaveDetails(action,xh,kch,skjc,skxq,skzc,xnxq).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LeaveDetailsBean>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_DAY_LEAVE_DETAILS_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"dayLeave(请假详情) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(LeaveDetailsBean bean) {
                        //返回数据处理
                        if (bean != null)
                            EventBus.getDefault().post(bean, Constant.BUS_DAY_LEAVE_DETAILS_LIST);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 日课表 随堂评价检测 数据处理请求
     *
     */
    public void evaluateTesting(String action,String pjzl,String xnxq,String xh,String jsh,String kch,
                                        String skjc,String skxq,String skzc,String szdd) {
        Subscription subscription = mScheduleAPI
                .evaluateTesting(action,pjzl,xnxq,xh,jsh,kch,skjc,skxq,skzc,szdd).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ScheduleTestingJsonPack>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_EVALUATE_TESTING_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"evaluateTesting(评价检测) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(ScheduleTestingJsonPack pack) {
                        //返回数据处理
                        if (pack != null)
                            EventBus.getDefault().post(pack, Constant.BUS_EVALUATE_TESTING_DETAILS_LIST);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 日课表 随堂评价提交 数据处理请求
     *
     */
    public void evaluate(String action,String pjzl,String xnxq,String xh,String jsh,String kch,String pjly,
                         String wd_ids,String fs,String skjc,String skxq,String skzc,String szdd) {
        Subscription subscription = mScheduleAPI
                .evaluate(action,pjzl,xnxq,xh,jsh,kch,pjly,wd_ids,fs,skjc,skxq,skzc,szdd).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<InfoReturnBean>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_EVALUATE_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"evaluateTesting(评价提交) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(InfoReturnBean pack) {
                        //返回数据处理
                        if (pack != null)
                            EventBus.getDefault().post(pack, Constant.BUS_EVALUATE_DETAILS_LIST);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 日课表 签到
     *
     */
    public void scheduleSign(String action,String xh,String jsh,String kch,String xnxq,
                         String skzc,String skxq,String skjc,String jxb){
        Subscription subscription = mScheduleAPI
                .scheduleSign(action,xh,jsh,kch,xnxq,skzc,skxq,skjc,jxb).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<InfoReturnBean>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_DAY_SCHEDULE_SIGN_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"homeSignMsg(日课表：签到) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(InfoReturnBean bean) {
                        //返回数据处理
                        if (bean != null)
                            EventBus.getDefault().post(bean, Constant.BUS_DAY_SCHEDULE_SIGN);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 周课表 数据处理请求
     *
     */
    public void scheduleWeekList(String action,String xh,String xnxq,String skzc) {
        Subscription subscription = mScheduleAPI
                .scheduleWeekList(action,xh,xnxq,skzc).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ScheduleWeekBean>>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_WEEK_SCHEDULE_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"scheduleList(周课表) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<ScheduleWeekBean> list) {
                        //返回数据处理
                        if (list != null)
                            EventBus.getDefault().post(list, Constant.BUS_WEEK_SCHEDULE_LIST);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }


    /**
     *
     * 周课表获取对应的日期 数据处理请求
     *
     */
    public void scheduleWeekDateList(String action,String xnxq,String skzc) {
        Subscription subscription = mScheduleAPI
                .scheduleWeekDateList(action,xnxq,skzc).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ScheduleWeekDateJsonPack>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_WEEK_DATE_SCHEDULE_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"scheduleWeekDateList(周课表对应日期) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(ScheduleWeekDateJsonPack jsonPack) {
                        //返回数据处理
                        if (jsonPack != null)
                            EventBus.getDefault().post(jsonPack, Constant.BUS_WEEK_DATE_SCHEDULE_LIST);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }


    public final int getTotalSize(){
        return size;
    }
}
